<template>
  <div>
    <publicPageVue :json-obj="pageSetting" name="TimerConfig" :tables="tables" ref="timerPage">
      <template #pageItme>
        <el-button @click="exportTime">导出</el-button>
        <el-upload :action="``" :before-upload="beforeAvatarUpload" class="upload-demo" name="uploadFile" :show-file-list="false">
          <el-button>导入</el-button>
        </el-upload>
      </template>
    </publicPageVue>
  </div>
</template>

<script>
  import { Mixin } from '@/common/minxin/Mixin'
  import { GFCD } from '@/utils/utils'
  import publicPageVue from '../../components/publicPage.vue'
  import { getList } from '../../api/home.js'
  import { getComponentsAll } from '@ve/utils/translateTools'
  import debounce from 'lodash/debounce'
  import TimerConfig from '../../components/json/TimerConfig'
  import { ungzip } from '@/utils/gzip.js'
  import { decryptd, encryptd } from '@/utils/tools'
  export default {
    name: 'TimerConfig',
    components: { publicPageVue },
    mixins: [Mixin],
    data() {
      return {
        demo: '',
        tables: {
          tableList: [],
          count: 0,
          pageSize: 50,
          limit: 1,
          name: 'TimerConfig',
          operation: false,
          isNew: true,
          width: '250',
        },
        configSetting: {},
        pageSetting: {
          search: {
            form: {
              field: [],
            },
          },
        },
        clientHeight: 0,
        tableData1: [],
      }
    },
    computed: {
      getHeightFuc() {
        return `${this.getHeight - 48}`
      },
      getHeight() {
        return `${this.clientHeight - 100}`
      },
    },
    created() {
      this.init()
      // this.getNewData()
    },
    methods: {
      async exportTime() {
        const checkboxList = this.$refs['timerPage'].$refs['main_Table'].$refs['xTableTimerConfig'].getCheckboxRecords()
        let method = 'dams.timer.export'
        let res = await getList(`${this.configSetting.domain}${this.configSetting.baseUrl}`, {
          method: method,
          data: JSON.stringify({
            '#LIMIT#': 1,
            '#PAGE_SIZE#': 9999,
            ID: checkboxList.map((el) => el.ID).join(','),
            SUB_SYSTEM: checkboxList.map((el) => el.SUB_SYSTEM).join(','),
            TIMER_NAME: checkboxList.map((el) => el.TIMER_NAME).join(','),
          }),
        })
        this.downloadTxt(res.data[method].data)
      },
      beforeAvatarUpload(file) {
        console.log(file)
        const reader = new FileReader()
        reader.readAsText(file, 'utf8') // input.files[0]为第一个文件
        reader.onload = () => {
          this.uploadTxt(reader.result)
        }
      },
      async uploadTxt(data) {
        let res = await getList(`${this.configSetting.domain}${this.configSetting.baseUrl}`, {
          method: 'dams.timer.import',
          data: decryptd(data),
        })
        this.$baseMessage(`操作成功!`, 'success')
      },
      downloadTxt(data) {
        const stringData = encryptd(JSON.stringify(data))
        const blob = new Blob([stringData], {
          type: 'text/plain;charset=utf-8',
        })
        const objectURL = URL.createObjectURL(blob)
        const aTag = document.createElement('a')
        this.$baseMessage(`操作成功!`, 'success')
        aTag.href = objectURL
        aTag.download = '定时器配置.txt'
        aTag.click()
        URL.revokeObjectURL(objectURL)
      },
      windowResize: debounce(function () {
        this.clientHeight = document.documentElement.clientHeight
      }, 100),
      async init() {
        this.configSetting = GFCD('dams')
        this.windowResize()
        let res = await this.getTimerGroup()
        let res1 = await this.getSubsystemData()
        let list = []
        let list1 = []
        res.forEach((item) => {
          list.push({ CODE: item.GROUP_NAME, LABEL: item.GROUP_CN_DESC, TIMER_HOST: item.TIMER_HOST })
        })
        res1.forEach((item) => {
          list1.push({ CODE: item.CODE, LABEL: item.LABEL })
        })
        // this.nowJsonData().then((data) => {
        //   console.log(data)
        this.pageSetting = TimerConfig
        let listField = this.pageSetting.TimerConfig.dialog.form.field
        let listField1 = this.pageSetting.TimerConfig.search.form.field
        listField.forEach((item) => {
          if (item.name == 'TIMER_GROUP') {
            item.options = list
          }
          if (item.name == 'SUB_SYSTEM') {
            item.options = list1
          }
        })
        listField1.forEach((item) => {
          if (item.name == 'SUB_SYSTEM') {
            item.options = list1
          }
        })
        // })
      },
      //获取定时器分组下拉数据
      async getTimerGroup() {
        let method = 'dams.app.timer.group.query'
        let res = await getList(`${this.configSetting.domain}${this.configSetting.baseUrl}`, {
          method: method,
          data: JSON.stringify({ '#LIMIT#': 1, '#PAGE_SIZE#': 9999 }),
        })
        return res.data[method].data
      },
      //获取子系统下拉数据
      async getSubsystemData() {
        let method = 'dams.handle.flow.query.options'
        let res = await getList(`${this.configSetting.domain}${this.configSetting.baseUrl}`, {
          method: method,
          data: JSON.stringify({ '#LIMIT#': 1, '#PAGE_SIZE#': 9999 }),
        })
        return res.data.SUB_SYSTEM.data
      },
      async getNewData() {
        let method = 'wms.query.pagejson'
        // let res = await getList(`http://192.168.3.222/wms/wms-web`, {
        //   method: method,
        //   data: JSON.stringify({ '#LIMIT#': 1, '#PAGE_SIZE#': 9999 }),
        // })
        let res = await getList(`http://192.168.3.222/wms/wms-web`, {
          data: JSON.stringify({ MENU_CODE: 'P1002', '#LIMIT#': 1, '#PAGE_SIZE#': 9999 }),
          method: method,
        })
        let pageJson = ungzip(res.data[method].data[0].PAGE_JSON)
        this.tableData1 = getComponentsAll(pageJson).components
        // return res.data[method].data
      },
    },
  }
</script>

<style lang="scss" scoped></style>
